let express = require('express')

var consolidate = require('consolidate')
var server = express()
//导入session模块
var session = require('express-session')
var flash = require('connect-flash')


var {query} = require('./utils/db.js')

//配置session

server.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true
    //cookie: { secure: true }   /*secure https这样的情况才可以访问cookie*/
}))

// 配置 消息提示 flash
server.use(flash())

// 分配公共页面需要的数据 session.user
server.use((req,res,next)=>{
    //success error 提示信息分配
    res.locals.success = req.flash('success').toString()
    res.locals.error = req.flash('error').toString()
    // 所有页面需要的公共数据 都存在 res.locals.键名 = 键值
    res.locals.user = req.session.user ? req.session.user : null  // 登录之后是 {username:''} 没登录 null
    next()
})


// 使用consolidate 配置ejs模板引擎

server.set('view engine','html');

server.set('views','./views');

server.engine('html',consolidate.ejs);

//设置网站的静态路径 css js
var path = require('path')
server.use(express.static(path.join(__dirname, 'public')));

//引入模块
const bodyParser = require('body-parser');
//定义urlencoded中间件
server.use(bodyParser.urlencoded({extended:1000*60}));



//文章相关操作路由
let articleRouter = require('./router/article.js')
server.use('/article',articleRouter)
//登录注册
let loginregRouter = require('./router/loginreg.js')
server.use('/user', loginregRouter)


// bootstrap学习路由
server.get('/bootstrap',(req,res)=>{
    res.render('bootstrap_learn.ejs')
})

// set PORT=9090
// node app.js
let port = process.env.PORT ||  8088

server.listen(port,function(){
    console.log('run at : ' + port)
})
